﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NopSolutions.NopCommerce.Controls
{
    public class NumericTextBox : TextBox
    {
        private RequiredFieldValidator _rfv;
        private RangeValidator _rv;
        private string _errorCssClass = "error";

        public NumericTextBox() {
            _rfv = new RequiredFieldValidator();
            _rv = new RangeValidator();
        }

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            _rfv.ControlToValidate = this.ID;
            _rfv.Display = ValidatorDisplay.Dynamic;
            _rfv.CssClass = ErrorCssClass;
            _rfv.ValidationGroup = ValidationGroup;
            Controls.Add(_rfv);

            _rv.ControlToValidate = this.ID;
            _rv.Display = ValidatorDisplay.Dynamic;
            _rv.Type = ValidationDataType.Integer;
            _rv.CssClass = ErrorCssClass;
            _rv.ValidationGroup = ValidationGroup;
            Controls.Add(_rv);
        }

        protected override void Render(HtmlTextWriter w) {
            base.Render(w);
            _rfv.RenderControl(w);
            _rv.RenderControl(w);
        }

        public string ErrorMessage {
            get { return _rfv.ErrorMessage; }
            set { _rfv.ErrorMessage = value; }
        }

        public string RangeErrorMessage {
            get { return _rv.ErrorMessage; }
            set { _rv.ErrorMessage = value; }
        }

        public string ErrorCssClass {
            get { return _errorCssClass; }
            set { _errorCssClass = value; }
        }

        public bool EnableClientScript {
            get { return _rfv.EnableClientScript; }
            set { _rfv.EnableClientScript = value; }
        }

        public string MinimumValue {
            get { return _rv.MinimumValue; }
            set { _rv.MinimumValue = value; }
        }

        public string MaximumValue {
            get { return _rv.MaximumValue; }
            set { _rv.MaximumValue = value; }
        }

        public int Value {
            get { return int.Parse(Text); }
            set { Text = value.ToString(); }
        }
    }
}
